function TLBusrepairDlg(action, varargin)

% TL_BUSREPAIR_DLG provides a user interface for bus repair report options
%
% SYNTAX TLBusrepairDlg('create')
%   creates a user interface for bus repair report options.
%   
%   INPUT ARGUMENTS
%   docNode     [optional] docNode which contains all information about changed bus signals
%
%   OUTPUT ARGUMENTS
%   - / -


% Author(s): D. Andoleit
% $RCSfile: TLBusrepairDlg.m $
% $ProjectName: e:/ARC/Components/HostSW/SimulinkBlocksets/TLBlocksets/CommonBlockset/CommonBlockset_GeneralUI/Sources/MSrc/project.pj $
% $Revision: 1.2 $
% $Date: 2011/07/01 08:26:27MESZ $
%
% Copyright ?2011 dSPACE GmbH. All rights reserved.


if strcmp(ds_error_get('Batchmode'),'on'),
    % dialog should not be displayed
    return
end

switch action
    case 'create'
        FcnCreate(varargin{:});
    case 'manage'
        dlgfig = varargin{1};
        dlgdata = get(dlgfig,'UserData');
        FcnManageControls(dlgfig, dlgdata, varargin{2});
end

        
%--------------------------------------------------------------------------
% FcnCreate creates bus repair dialog
% 
% INPUT ARGUMENTS
%   docNode         docNode which contains all information about changed bus signals
%
% OUTPUT ARGUMENTS
%   - / -
%--------------------------------------------------------------------------
function FcnCreate(varargin)

if nargin == 0
    docNode = [];
else
    docNode = varargin{1};
end

dlgfig = figure( ...
    'DefaultUicontrolHorizontalAlign',   'left',...
    'Visible',                           'off',...
    'DefaultUicontrolUnits',             'pixels',...
    'DefaultUicontrolInterruptible',     'off',...
    'HandleVisibility',                  'off',...
    'Colormap',                          [],...
    'Toolbar',                           'none', ...
    'MenuBar',                           'none', ...
    'DockControls',                      'off', ...
    'NumberTitle',                       'off', ...
    'Name',                              'TargetLink bus repair report',...
    'IntegerHandle',                     'off',...
    'Interruptible',                     'off',...
    'Tag',                               'TargetLink Bus Repair Dialog', ...
    'BusyAction',                        'queue',...
    'Resize',                            'off', ...
    'Color',                             get(0,'DefaultuicontrolBackGroundColor'), ...
    'KeyPressFcn',                       ' ',...
    'CloseRequestFcn',                   'TLBusrepairDlg(''manage'',gcbf,''Close'')');

% Construct common geometry constants.

commonGeom = ds_get_common_geometry;
set(dlgfig,...
    'DefaultUicontrolFontname',commonGeom.fontname,...
    'DefaultUicontrolFontsize',commonGeom.fontsize);

% now create the controls
strings_and_styles = {...
    repmat('m',1,40),   'text'; ...
    'Exclude signals thats index in bus hierachy was changed',      'checkbox'; ...
    ' Show report ',    'pushbutton'};
[ ...
    busrepairTextSize, ...
    excludeIdxCheckboxSize, ...
    showReportButtonSize] ...
    = ds_get_uicontrol_sizes(commonGeom,strings_and_styles);

[col,colWidth,colDelta,rowDelta] = ds_get_gui_columns(busrepairTextSize(1), commonGeom);

cy = commonGeom.sheetBottomEdgeBuffer;
cx = col(1) + colWidth(1) - 2*showReportButtonSize(1);
Children.showReportButton = uicontrol( ...
    'Parent',           dlgfig, ...
    'ToolTipString',    'Opens a report file of bus repair changes.', ...
    'Style',            'pushbutton', ...
    'String',           ' Show report ', ...
    'Position',         [cx cy showReportButtonSize]);

cx = col(1) + colWidth(1) - showReportButtonSize(1) + colDelta/3;
Children.finishButton = uicontrol( ...
    'Parent',           dlgfig, ...
    'ToolTipString',    'Close the dialog and delete the bus repair log file.', ...
    'Style',            'pushbutton', ...
    'String',           ' Finish' , ...
    'Position',         [cx cy showReportButtonSize]);

cx = col(1);
cy = cy + commonGeom.editHeight;

cy = cy + commonGeom.editHeight + rowDelta;
Children.excludeIdxCheckbox = uicontrol( ...
    'Parent',           dlgfig, ...
    'Style',            'checkbox', ...
    'ToolTipString',    'Exclude signals thats index in bus hierachy was changed', ...
    'String',           'Exclude signals thats index in bus hierachy was changed', ...
    'Position',         [cx cy excludeIdxCheckboxSize]);

cy = cy + commonGeom.editHeight + rowDelta;
Children.busrepairText = uicontrol( ...
    'Parent',           dlgfig, ...
    'Style',            'text', ...
    'string',           'TargetLink BusPort blocks were analyzed.', ...
    'Position',         [cx cy busrepairTextSize(1) busrepairTextSize(2)]);

dlgdata.children = Children;
dlgdata.docNode = docNode;
dlgdata.excludeIdx = false;

% Place dSPACE logo
cx = col(1) + colDelta/5;
cy = commonGeom.sheetBottomEdgeBuffer + commonGeom.sysButton_SheetDelta;
ds_create_logo(dlgfig,cx,cy);

width = ...
    2*commonGeom.frameSideEdgeBuffer + ...
    busrepairTextSize(1);

height = ...
    cy + ...
    3*commonGeom.editHeight + rowDelta;

sheetDims = [width height];
figPos = get(dlgfig,'Position');
figHeight = commonGeom.figBottomEdgeBuffer + ...
    sheetDims(2) + ...
    commonGeom.figTopEdgeBuffer + ...
    commonGeom.editHeight;    % status label
figWidth = 2*commonGeom.figSideEdgeBuffer + sheetDims(1);

figPos(3:4) = [figWidth, figHeight];
screensize = get(0,'ScreenSize');
if figPos(1) + figPos(3) > screensize(3)
    figPos(1) = screensize(3) - figPos(3) - 20;
end
if figPos(2) + figPos(4) + 50 > screensize(4)
    figPos(2) = screensize(4) - figPos(4) - 50;
end

set(dlgfig,'Position',figPos);

% set callbacks
children = struct2cell(dlgdata.children);
children = [children{:}];
fields = fieldnames(dlgdata.children);
for k = 1 : numel(children)
        set(children(k),'callback',['TLBusrepairDlg(''manage'',gcbf,''' fields{k} ''');']);
end

FcnUpdate(dlgfig,dlgdata);

% make visible
set(dlgfig,'DefaultUicontrolVisible','on','Visible','on');


%================================================================================
% FcnManageControls manages callbacks for controls
%
% Input parameters
%   dlgfig    dialog figure handle
%   dlgdata   dialog user data
%   action    name of control = name of field in dlgdata.children struct
%================================================================================
function FcnManageControls(dlgfig, dlgdata, action)

bModified = false;
Children = dlgdata.children;

switch action
    case 'excludeIdxCheckbox'
        dlgdata.excludeIdx = get(Children.excludeIdxCheckbox, 'Value');
        bModified = true;
        
    case 'showReportButton'
        delete(dlgfig);
        tl_busrepairreport('docNode', dlgdata.docNode, 'excludeindexchanges', dlgdata.excludeIdx);
        
    case 'finishButton'
        delete(dlgfig);

    case {'Close','Cancel','CloseReq'}
        set(dlgfig,'Visible','off');
        delete(dlgfig);

    otherwise
        error(['Invalid action ' Action]);

end

if bModified
    FcnUpdate(dlgfig,dlgdata);
end


%================================================================================
% Function updates controls and checks options.
%
% Input parameters
%   dlgfig    dialog figure handle
%   dlgdata   dialog user data
%================================================================================
function FcnUpdate(dlgfig,dlgdata)

set(dlgfig,'UserData',dlgdata);

